﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;

namespace FasterWPF
{
    public static class DataGridExt
    {
        public static void ApplyDataGridStrategy(this DataGrid dg, double rowHeaderWidth, DataGridSelectionMode selectionMode, DataGridSelectionUnit selectionUnit, Brush regularRowColor, Brush selectedCellColor, Brush selectedCellForeground, Brush selectedCellFocusBorder, string rowHeaderBackground, string rowHeaderBorderBrush, string borderThickness, double separatorHeight, string separatorBrush, string mouseOverRowHeaderHighlightColor, string mouseOverRowHeaderBorderColor, string selectedRowHeaderColor, string selectedRowHeaderBorderColor)
        {
            dg.RowHeaderWidth = rowHeaderWidth;
            dg.SelectionMode = selectionMode;
            dg.SelectionUnit = selectionUnit;
            dg.SetRegularRowColor(regularRowColor);
            dg.SetSelectedContentControlColor(selectedCellColor);
            dg.SetSelectedContentControlForeground(selectedCellForeground);
            dg.SetSelectedFocusBorder(selectedCellFocusBorder);

            string xaml = "<Style x:Key=\"{x:Type DataGridRowHeader}\" TargetType=\"{x:Type DataGridRowHeader}\"><Style.Resources><Style x:Key=\"RowHeaderGripperStyle\" TargetType=\"{x:Type Thumb}\"><Setter Property=\"Height\" Value=\"" + separatorHeight + "\"/><Setter Property=\"Background\" Value=\"" + separatorBrush + "\"/><Setter Property=\"Cursor\" Value=\"SizeNS\"/><Setter Property=\"Template\"><Setter.Value><ControlTemplate TargetType=\"{x:Type Thumb}\"><Border Padding=\"{TemplateBinding Padding}\" Background=\"{TemplateBinding Background}\"/></ControlTemplate></Setter.Value></Setter></Style></Style.Resources><Setter Property=\"Template\"><Setter.Value><ControlTemplate TargetType=\"{x:Type DataGridRowHeader}\"><Grid><theme:DataGridHeaderBorder Name=\"GridHeaderBorder\" IsSelected=\"{TemplateBinding IsRowSelected}\" IsHovered =\"{TemplateBinding IsMouseOver}\" IsPressed=\"{TemplateBinding IsPressed}\" Background=\"" + rowHeaderBackground + "\" BorderBrush=\"" + rowHeaderBorderBrush + "\" BorderThickness=\"" + borderThickness + "\" Padding =\"{TemplateBinding Padding}\" Orientation=\"Horizontal\" SeparatorVisibility=\"Visible\" SeparatorBrush=\"Gray\"><StackPanel Orientation=\"Horizontal\"><ContentPresenter SnapsToDevicePixels=\"{TemplateBinding SnapsToDevicePixels}\" VerticalAlignment=\"Center\"/></StackPanel></theme:DataGridHeaderBorder><Thumb x:Name=\"PART_TopHeaderGripper\" VerticalAlignment=\"Top\" Style=\"{StaticResource RowHeaderGripperStyle}\"/><Thumb x:Name=\"PART_BottomHeaderGripper\" VerticalAlignment=\"Bottom\" Style=\"{StaticResource RowHeaderGripperStyle}\"/></Grid><ControlTemplate.Triggers><Trigger Property=\"IsMouseOver\" Value=\"true\"><Setter TargetName=\"GridHeaderBorder\" Property=\"BorderBrush\" Value=\"" + mouseOverRowHeaderBorderColor + "\"/><Setter TargetName=\"GridHeaderBorder\" Property=\"Background\" Value=\"" + mouseOverRowHeaderHighlightColor + "\"/></Trigger><Trigger Property=\"IsRowSelected\" Value=\"true\"><Setter TargetName=\"GridHeaderBorder\" Property=\"BorderBrush\" Value=\"" + selectedRowHeaderBorderColor + "\"/><Setter TargetName=\"GridHeaderBorder\" Property=\"Background\" Value=\"" + selectedRowHeaderColor + "\"/></Trigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style>";

            MemoryStream sr = new MemoryStream(Encoding.ASCII.GetBytes(xaml));
            ParserContext pc = new ParserContext();
            pc.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
            pc.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
            pc.XmlnsDictionary.Add("theme", "clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero");

            var rowStyle = (Style)XamlReader.Load(sr, pc);

            dg.RowHeaderStyle = rowStyle;
        }

        public static DataGrid SetBackgroundColor(this DataGrid chain, Brush brush)
        {
           // StyleExt.SetStyle(chain, typeof(DataGrid), DataGrid.BackgroundProperty, brush);
           
            Brush newBrush = (brush);
            chain.Background = newBrush;
            return chain;
        }

        public static DataGrid SetRegularRowColor(this DataGrid chain, Brush color)
        {
            chain.Resources.Add(SystemColors.WindowBrushKey, color);
            return chain;
        }

        public static DataGrid SetColumnHeaderForeground(this DataGrid chain, Brush color)
        {
            StyleExt.SetStyle(chain, typeof(DataGridColumnHeader), DataGridColumnHeader.ForegroundProperty, color);
            return chain;
        }

        public static DataGrid SetColumnHeaderColor(this DataGrid chain, Brush color)
        {
            StyleExt.SetStyle(chain, typeof(DataGridColumnHeader), DataGridColumnHeader.BackgroundProperty, color);
            return chain;
        }
             
        public static DataGrid SetSelectedContentControlColor(this DataGrid chain, Brush color)
        {
            chain.Resources.Add(SystemColors.HighlightBrushKey, color);
            chain.Resources.Add(SystemColors.ControlBrushKey, color);
            return chain;
        }

        public static DataGrid SetSelectedFocusBorder(this DataGrid chain, Brush color)
        {
            chain.Resources.Add(DataGrid.FocusBorderBrushKey, color);
            return chain;
        }

        public static DataGrid SetSelectedContentControlForeground(this DataGrid chain, Brush color)
        {
            chain.Resources.Add(SystemColors.HighlightTextBrushKey, color);
            return chain;
        }

        public static object GetRow(this DataGrid grid, int index)
        {
            object row = null;

            row = grid.ItemContainerGenerator.ContainerFromIndex(index);
            if (row == null)
            {
                // May be virtualized, bring into view and try again.
                grid.UpdateLayout();

                if ((grid.Items != null) && (grid.Items.Count > 0) && ((grid.Items.Count)>index))
                {
                    grid.ScrollIntoView(grid.Items[index]);
                }

                row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
            }


            if ((row == null) && (grid != null) && (grid.Items != null) && (grid.Items.Count > 0) && ((grid.Items.Count)
                > index))
            {
                if ((grid).Items[index].GetType().Name != "NamedObject")
                {
                    row = (grid).Items[index];
                }
            }

            return row;

        }

        public static void SelectFirstItemInGrid(this DataGrid sourceDataGrid, StackPanel mainPanel) 
        {
           if(sourceDataGrid.Items!=null && sourceDataGrid.Items.Count>0)
           {
               ScrollToLastItemInGrid(sourceDataGrid);       
               sourceDataGrid.SelectedIndex=0;
               sourceDataGrid.ScrollIntoView(sourceDataGrid.Items.GetItemAt(0));
           }
        }

        public static void ScrollToLastItemInGrid(this DataGrid sourceDataGrid)
        {
            sourceDataGrid.SetValue(DataGrid.EnableRowVirtualizationProperty, false);
            sourceDataGrid.SetValue(DataGrid.EnableColumnVirtualizationProperty, false);
            sourceDataGrid.SetValue(VirtualizingStackPanel.IsVirtualizingProperty, false);

            sourceDataGrid.SelectedIndex=(sourceDataGrid.Items.Count - 1);
            if(sourceDataGrid.Items.Count!=0)
            {
               sourceDataGrid.ScrollIntoView(sourceDataGrid.Items.GetItemAt(sourceDataGrid.Items.Count - 1));
            }
        }
        
        /// <summary>
        /// After adding a row, you may need to call Refresh
        /// </summary>
        /// <param name="sourceDataGrid"></param>
        public static void Refresh(this DataGrid sourceDataGrid)
        {
            System.Windows.Data.CollectionViewSource.GetDefaultView(sourceDataGrid.ItemsSource).Refresh();
        }

        //source: http://stackoverflow.com/questions/8464704/get-row-in-datagrid
        public static DataGridRow GetSelectedRow(this DataGrid grid)
        {
            return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
        }
        ////source: http://stackoverflow.com/questions/8464704/get-row-in-datagrid
        //public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
        //{
        //    if (row != null)
        //    {
        //        DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

        //        if (presenter == null)
        //        {
        //            grid.ScrollIntoView(row, grid.Columns[column]);
        //            presenter = GetVisualChild<DataGridCellsPresenter>(row);
        //        }

        //        DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
        //        return cell;
        //    }
        //    return null;
        //}
        //source: http://stackoverflow.com/questions/8464704/get-row-in-datagrid
        //public static DataGridCell GetCell(this DataGrid grid, int row, int column)
        //{
        //    DataGridRow rowContainer = grid.GetRow(row);
        //    return grid.GetCell(rowContainer, column);
        //}

        //public static DataGrid Initialize(this DataGrid source, ContainerType containerType)
        //{  
        //       source.Initialize(double.NaN, double.NaN, containerType);
        //       return source;
        //}

        //public static DataGrid Initialize(this DataGrid source, ContainerType containerType, int NumRows, int NumColumns, double colWidthToRepeat, GridUnitType unitTypeToRepeat)
        //{
        //       source.Initialize(double.NaN, double.NaN, containerType, NumRows, NumColumns, colWidthToRepeat, unitTypeToRepeat);
        //       return source;
        //}

        //public static Grid Initialize(this Grid source, ContainerType containerType, int NumRows, int NumColumns, double colWidth1, GridUnitType unitType1,
        //   double colWidth2 = 9999999999D, GridUnitType unitType2 = GridUnitType.Auto,
        //   double colWidth3 = 9999999999D, GridUnitType unitType3 = GridUnitType.Auto,
        //   double colWidth4 = 9999999999D, GridUnitType unitType4 = GridUnitType.Auto,
        //   double colWidth5 = 9999999999D, GridUnitType unitType5 = GridUnitType.Auto,
        //   double colWidth6 = 9999999999D, GridUnitType unitType6 = GridUnitType.Auto,
        //   double colWidth7 = 9999999999D, GridUnitType unitType7 = GridUnitType.Auto,
        //   double colWidth8 = 9999999999D, GridUnitType unitType8 = GridUnitType.Auto,
        //   double colWidth9 = 9999999999D, GridUnitType unitType9 = GridUnitType.Auto,
        //   double colWidth10 = 9999999999D, GridUnitType unitType10 = GridUnitType.Auto,
        //   double colWidth11 = 9999999999D, GridUnitType unitType11 = GridUnitType.Auto,
        //   double colWidth12 = 9999999999D, GridUnitType unitType12 = GridUnitType.Auto,
        //   double colWidth13 = 9999999999D, GridUnitType unitType13 = GridUnitType.Auto,
        //   double colWidth14 = 9999999999D, GridUnitType unitType14 = GridUnitType.Auto,
        //   double colWidth15 = 9999999999D, GridUnitType unitType15 = GridUnitType.Auto,
        //   double colWidth16 = 9999999999D, GridUnitType unitType16 = GridUnitType.Auto,
        //   double colWidth17 = 9999999999D, GridUnitType unitType17 = GridUnitType.Auto,
        //   double colWidth18 = 9999999999D, GridUnitType unitType18 = GridUnitType.Auto,
        //   double colWidth19 = 9999999999D, GridUnitType unitType19 = GridUnitType.Auto,
        //   double colWidth20 = 9999999999D, GridUnitType unitType20 = GridUnitType.Auto,
        //   double colWidth21 = 9999999999D, GridUnitType unitType21 = GridUnitType.Auto,
        //   double colWidth22 = 9999999999D, GridUnitType unitType22 = GridUnitType.Auto,
        //   double colWidth23 = 9999999999D, GridUnitType unitType23 = GridUnitType.Auto,
        //   double colWidth24 = 9999999999D, GridUnitType unitType24 = GridUnitType.Auto,
        //   double colWidth25 = 9999999999D, GridUnitType unitType25 = GridUnitType.Auto)
        //        {
        //            source.Initialize(double.NaN, double.NaN, containerType, NumRows, NumColumns, colWidth1, unitType1,
        //   colWidth2, unitType2,
        //   colWidth3, unitType3,
        //   colWidth4, unitType4,
        //   colWidth5, unitType5,
        //   colWidth6, unitType6,
        //   colWidth7, unitType7,
        //   colWidth8, unitType8,
        //   colWidth9, unitType9,
        //   colWidth10, unitType10,
        //   colWidth11, unitType11,
        //   colWidth12, unitType12,
        //   colWidth13, unitType13,
        //   colWidth14, unitType14,
        //   colWidth15, unitType15,
        //   colWidth16, unitType16,
        //   colWidth17, unitType17,
        //   colWidth18, unitType18,
        //   colWidth19, unitType19,
        //   colWidth20, unitType20,
        //   colWidth21, unitType21,
        //   colWidth22, unitType22,
        //   colWidth23, unitType23,
        //   colWidth24, unitType24,
        //   colWidth25, unitType25);
        //            return source;
        //}

        public static DataGrid Initialize(this DataGrid source, double width, double height, ContainerType containerType, Brush alternatingRowBackground, Brush horizontalGridLinesBrush, Brush verticalGridLinesBrush, Thickness dataGridCellBorderThickness, int? rowSpan, int? colSpan)
        {
            if (rowSpan != null)
            {
                Grid.SetRowSpan(source, (int)rowSpan);
            }

            if (colSpan != null)
            {
                Grid.SetColumnSpan(source, (int)colSpan);
            }

            return Initialize(source, width, height, containerType, alternatingRowBackground, horizontalGridLinesBrush, verticalGridLinesBrush, dataGridCellBorderThickness); //Default width and height to Auto by setting to NaN
        }

        public static DataGrid Initialize(this DataGrid source, double width, double height, ContainerType containerType, Brush alternatingRowBackground, Brush horizontalGridLinesBrush, Brush verticalGridLinesBrush, Thickness dataGridCellBorderThickness)
        {            
            FrameworkElementFactory factory = new FrameworkElementFactory(typeof(StackPanel));
            factory.SetValue(VirtualizingStackPanel.IsVirtualizingProperty, false);   
            source.SetValue(VirtualizingStackPanel.IsVirtualizingProperty, false);
            source.ItemsPanel = new ItemsPanelTemplate(factory);

            //Force the Aero theme by default to assure the structure of the Listbox
            Uri uri = new Uri("/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml", UriKind.Relative);
            source.Resources.MergedDictionaries.Add(Application.LoadComponent(uri) as ResourceDictionary);

            source.SetValue(DataGrid.EnableRowVirtualizationProperty, false);
            source.SetValue(DataGrid.EnableColumnVirtualizationProperty, false);
            source.SetValue(VirtualizingStackPanel.IsVirtualizingProperty, false);

            source.CanUserAddRows = false;
            source.Width = width;
            source.Height = height;
            source.SetContainerType(containerType);
            IComposite icomp = CompositeTypeHelper.CreateIComposite<DataGrid>(source);
            source.SetIComposite<DataGrid>(icomp);

            source.AutoGenerateColumns = false;
            source.AlternatingRowBackground = alternatingRowBackground;
            source.HorizontalGridLinesBrush = horizontalGridLinesBrush;
            source.VerticalGridLinesBrush = verticalGridLinesBrush;

            DataGridParent gbsr = new DataGridParent();
            source.StoreSetting<DataGrid>("IParent", gbsr);
            source.StoreSetting<DataGrid>("IScrollViewerStrategy", gbsr);

            source.AddItemBorder(Brushes.Gray, new Thickness(1, 1, 1, 1)); //default Border     
            source.StoreSetting<DataGrid>("BorderSettings", new BorderSettings() { Width = double.NaN, Height = double.NaN, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(0, 0, 0, 0), CornerRadius = new CornerRadius(0), Padding = new Thickness(0) });

            source.SetStyle(typeof(DataGridCell), Control.BorderThicknessProperty, dataGridCellBorderThickness);

            ScrollViewer closeOverViewer = source.GetScrollViewer();
            closeOverViewer.CanContentScroll = false;  //Logical scrolling seems buggy. Therefore, always force physical scrolling
            
            //source.StoreSetting<DataGrid>("ScrollViewerDeltaStep", 1);
            //source.StoreSetting<DataGrid>("HorizScrollViewerDeltaStep", 1);

            //source.StoreSetting<DataGrid>("ScrollViewerScrollDownStep", 1);
            //source.StoreSetting<DataGrid>("ScrollViewerScrollDownDelay", 1000);
            //source.StoreSetting<DataGrid>("ScrollViewerScrollDownInterval", 100);
            //source.StoreSetting<DataGrid>("ScrollViewerPageDownStep", 1);
            //source.StoreSetting<DataGrid>("ScrollViewerPageDownDelay", 1000);
            //source.StoreSetting<DataGrid>("ScrollViewerPageDownInterval", 100);

            //source.StoreSetting<DataGrid>("ScrollViewerPageUpStep", 1);
            //source.StoreSetting<DataGrid>("ScrollViewerPageUpDelay", 1000);
            //source.StoreSetting<DataGrid>("ScrollViewerPageUpInterval", 100);
            //source.StoreSetting<DataGrid>("ScrollViewerScrollUpStep", 1);
            //source.StoreSetting<DataGrid>("ScrollViewerScrollUpDelay", 1000);
            //source.StoreSetting<DataGrid>("ScrollViewerScrollUpInterval", 100);

            //source.StoreSetting<DataGrid>("ScrollViewerScrollRightStep", 1);
            //source.StoreSetting<DataGrid>("ScrollViewerScrollRightDelay", 1000);
            //source.StoreSetting<DataGrid>("ScrollViewerScrollRightInterval", 100);
            //source.StoreSetting<DataGrid>("ScrollViewerPageRightStep", 1);
            //source.StoreSetting<DataGrid>("ScrollViewerPageRightDelay", 1000);
            //source.StoreSetting<DataGrid>("ScrollViewerPageRightInterval", 100);

            //source.StoreSetting<DataGrid>("ScrollViewerPageLeftStep", 1);
            //source.StoreSetting<DataGrid>("ScrollViewerPageLeftDelay", 1000);
            //source.StoreSetting<DataGrid>("ScrollViewerPageLeftInterval", 100);
            //source.StoreSetting<DataGrid>("ScrollViewerScrollLeftStep", 1);
            //source.StoreSetting<DataGrid>("ScrollViewerScrollLeftDelay", 1000);
            //source.StoreSetting<DataGrid>("ScrollViewerScrollLeftInterval", 100);

            source.StoreSetting<DataGrid>("ObsCol", new ObservableCollection<ColumnContent<Border, Border, Border, Border, Border, Border, Border, Border, Border, Border, Border>>());

            //ScrollViewerExt.SetVerticalScrollButtonDownHandler(source);
            //ScrollViewerExt.SetVerticalPageButtonDownHandler(source);
            //ScrollViewerExt.SetHorizontalScrollButtonRightHandler(source);
            //ScrollViewerExt.SetHorizontalPageButtonRightHandler(source);

            //ScrollViewerExt.SetVerticalPageButtonUpHandler(source);
            //ScrollViewerExt.SetHorizontalScrollButtonLeftHandler(source);
            //ScrollViewerExt.SetHorizontalPageButtonLeftHandler(source);

            //ScrollViewerExt.SetThumbDragDeltaHandler(source, gbsr);
            //ScrollViewerExt.SetHorizThumbDragDeltaHandler(source, gbsr);

            return source;
        }

        //public static DataGrid Initialize(this DataGrid source, double width, double height, ContainerType containerType, int NumRows, int NumColumns, double colWidthToRepeat, GridUnitType unitTypeToRepeat)
        //{
        //    source.Initialize(width, height, containerType);

        //    IterHelpers.ApplyLoop(NumRows, (i =>
        //    {
        //        source.RowDefinitions.Add(new RowDefinition() { MinHeight = 50D });
        //    }));

        //    IterHelpers.ApplyLoop(NumColumns, (i =>
        //    {
        //        ColumnDefinition c = new ColumnDefinition();
        //        c.Width = new GridLength(colWidthToRepeat,unitTypeToRepeat);
        //        source.ColumnDefinitions.Add(c);
        //    }));

        //    return source;
        //}

//        public static Grid Initialize(this Grid source, double width, double height, ContainerType containerType, int NumRows, int NumColumns, double colWidth1, GridUnitType unitType1,
//           double colWidth2 = 9999999999D, GridUnitType unitType2 = GridUnitType.Auto,
//           double colWidth3 = 9999999999D, GridUnitType unitType3 = GridUnitType.Auto,
//           double colWidth4 = 9999999999D, GridUnitType unitType4 = GridUnitType.Auto,
//           double colWidth5 = 9999999999D, GridUnitType unitType5 = GridUnitType.Auto,
//           double colWidth6 = 9999999999D, GridUnitType unitType6 = GridUnitType.Auto,
//           double colWidth7 = 9999999999D, GridUnitType unitType7 = GridUnitType.Auto,
//           double colWidth8 = 9999999999D, GridUnitType unitType8 = GridUnitType.Auto,
//           double colWidth9 = 9999999999D, GridUnitType unitType9 = GridUnitType.Auto,
//           double colWidth10 = 9999999999D, GridUnitType unitType10 = GridUnitType.Auto,
//           double colWidth11 = 9999999999D, GridUnitType unitType11 = GridUnitType.Auto,
//           double colWidth12 = 9999999999D, GridUnitType unitType12 = GridUnitType.Auto,
//           double colWidth13 = 9999999999D, GridUnitType unitType13 = GridUnitType.Auto,
//           double colWidth14 = 9999999999D, GridUnitType unitType14 = GridUnitType.Auto,
//           double colWidth15 = 9999999999D, GridUnitType unitType15 = GridUnitType.Auto,
//           double colWidth16 = 9999999999D, GridUnitType unitType16 = GridUnitType.Auto,
//           double colWidth17 = 9999999999D, GridUnitType unitType17 = GridUnitType.Auto,
//           double colWidth18 = 9999999999D, GridUnitType unitType18 = GridUnitType.Auto,
//           double colWidth19 = 9999999999D, GridUnitType unitType19 = GridUnitType.Auto,
//           double colWidth20 = 9999999999D, GridUnitType unitType20 = GridUnitType.Auto,
//           double colWidth21 = 9999999999D, GridUnitType unitType21 = GridUnitType.Auto,
//           double colWidth22 = 9999999999D, GridUnitType unitType22 = GridUnitType.Auto,
//           double colWidth23 = 9999999999D, GridUnitType unitType23 = GridUnitType.Auto,
//           double colWidth24 = 9999999999D, GridUnitType unitType24 = GridUnitType.Auto,
//           double colWidth25 = 9999999999D, GridUnitType unitType25 = GridUnitType.Auto)
//         {
//             source.Initialize(width, height, containerType);

//            List<double> colWidths = new List<double>();
//            List<GridUnitType> colUnitTypes = new List<GridUnitType>();
//            colWidths.Add(colWidth1);
//            colUnitTypes.Add(unitType1);
//            if (colWidth2 != 9999999999D) { colWidths.Add(colWidth2); colUnitTypes.Add(unitType2); }
//            if (colWidth3 != 9999999999D) { colWidths.Add(colWidth3); colUnitTypes.Add(unitType3); }
//            if (colWidth4 != 9999999999D) { colWidths.Add(colWidth4); colUnitTypes.Add(unitType4); }
//            if (colWidth5 != 9999999999D) { colWidths.Add(colWidth5); colUnitTypes.Add(unitType5); }
//            if (colWidth6 != 9999999999D) { colWidths.Add(colWidth6); colUnitTypes.Add(unitType6); }
//            if (colWidth7 != 9999999999D) { colWidths.Add(colWidth7); colUnitTypes.Add(unitType7); }
//            if (colWidth8 != 9999999999D) { colWidths.Add(colWidth8); colUnitTypes.Add(unitType8); }
//            if (colWidth9 != 9999999999D) { colWidths.Add(colWidth9); colUnitTypes.Add(unitType9); }
//            if (colWidth10 != 9999999999D) { colWidths.Add(colWidth10); colUnitTypes.Add(unitType10); }
//            if (colWidth11 != 9999999999D) { colWidths.Add(colWidth11); colUnitTypes.Add(unitType11); }
//            if (colWidth12 != 9999999999D) { colWidths.Add(colWidth12); colUnitTypes.Add(unitType12); }
//            if (colWidth13 != 9999999999D) { colWidths.Add(colWidth13); colUnitTypes.Add(unitType13); }
//            if (colWidth14 != 9999999999D) { colWidths.Add(colWidth14); colUnitTypes.Add(unitType14); }
//            if (colWidth15 != 9999999999D) { colWidths.Add(colWidth15); colUnitTypes.Add(unitType15); }
//            if (colWidth16 != 9999999999D) { colWidths.Add(colWidth16); colUnitTypes.Add(unitType16); }
//            if (colWidth17 != 9999999999D) { colWidths.Add(colWidth17); colUnitTypes.Add(unitType17); }
//            if (colWidth18 != 9999999999D) { colWidths.Add(colWidth18); colUnitTypes.Add(unitType18); }
//            if (colWidth19 != 9999999999D) { colWidths.Add(colWidth19); colUnitTypes.Add(unitType19); }
//            if (colWidth20 != 9999999999D) { colWidths.Add(colWidth20); colUnitTypes.Add(unitType20); }
//            if (colWidth21 != 9999999999D) { colWidths.Add(colWidth21); colUnitTypes.Add(unitType21); }
//            if (colWidth22 != 9999999999D) { colWidths.Add(colWidth22); colUnitTypes.Add(unitType22); }
//            if (colWidth23 != 9999999999D) { colWidths.Add(colWidth23); colUnitTypes.Add(unitType23); }
//            if (colWidth24 != 9999999999D) { colWidths.Add(colWidth24); colUnitTypes.Add(unitType24); }
//            if (colWidth25 != 9999999999D) { colWidths.Add(colWidth25); colUnitTypes.Add(unitType25); }

//            GridSettings gs = new GridSettings()
//            {
//                NumColumns =
//                    NumColumns,
//                NumRows = NumRows, 
//                ColWidths = colWidths, 
//                ColUnitTypes =
//                    colUnitTypes
//            };

//            IterHelpers.ApplyLoop(gs.NumRows, (i =>
//            {
//                source.RowDefinitions.Add(new RowDefinition() { MinHeight = 50D });
//            }));

//            IterHelpers.ApplyLoop(gs.NumColumns, (i =>
//            {
//                ColumnDefinition c = new ColumnDefinition();
//                c.Width = new GridLength(gs.ColWidths[i],
//gs.ColUnitTypes[i]);
//                source.ColumnDefinitions.Add(c);
//            }));

//            return source;
//        }


    }//end of class

}//end of namespace
